﻿using FunctionalCLR.Core;

namespace FunctionalCLR.Shell {
    public class ContextParser : Parser {

        protected IContext context = null;

        public ContextParser(string expr, IContext context)
            : base(expr)
        {
            this.context = context;
        }

        public override IdentifierNode GetIdenitiferNode(string value)
        {
            if (value.ToLower().Equals("true")
                || value.ToLower().Equals("false")) {
                IdentifierNode node = new IdentifierNode();
                node.Value = value;

                return node;
            }

            VariableNode fn = new VariableNode();
            fn.Context = this.context;
            fn.Variable = (Variable)this.context.GetVariableByName(value);
            if (fn.Variable == null) {
                fn.Variable = (Variable)context.AddVariable(value);
            }
            
            return fn;
        }
    }
}
